package com.br4mmie.asciistory.battle;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;

import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.FileGetter;
import com.br4mmie.asciistory.mission.Item;


/**
 * EnemyType Class
 * @author Niels
 * Describes a type of enemy as loaded from a file.
 */
public class EnemyType 
{
	private CharImage image; //this types' image
	private ArrayList<ScriptedAction> actions; //this types scripted actions
	private ArrayList<BattleDrop> drops; // the drops for creatures of this type
	private String name; // the displayed name of this creature type
	
	private int hp; // the max hp for creatures of this type
	private int xpValue; // the xpvalue for creatures of this type
	private int goldValue; //the amount of gold creatures of this type drop
	private int bonusDamage; // the added damage value for creatures of this type
	private int defense; // the defense value for creatures of this type
	
	/**
	 * Constructor, loads the type from file
	 * @param filename File to  load type from
	 */
	public EnemyType(String filename)
	{
		drops = new ArrayList<BattleDrop>();
		actions = new ArrayList<ScriptedAction>();
		loadFile(filename);
		
	}
	
	/**
	 * loadFile function, loads the type from the given filename
	 * @param filename
	 */
	private void loadFile(String filename) 
	{
		String imageFile = ""; //file containing the creatures CharImage 
		int numberOfDrops = 0; //used to check how many lines contain drops
		int numberOfActions = 0; //used to check how many lines contain abilities
		String line;
		
		BufferedReader in;
		try
		{
    		in = new BufferedReader(new FileReader(FileGetter.getFilename(filename)));
    		name = in.readLine();
    		hp = Integer.parseInt(in.readLine());
    		bonusDamage = Integer.parseInt(in.readLine());
    		defense = Integer.parseInt(in.readLine());
    		xpValue = Integer.parseInt(in.readLine());
    		goldValue = Integer.parseInt(in.readLine());
    		imageFile = in.readLine();
    		
    		line = in.readLine();
    		
    		numberOfDrops = Integer.parseInt(line.substring(line.indexOf(" ")+1)); //How many Battledrops to load
    		for (int i=0; i<numberOfDrops; i++)
    		{
    			String itemFile;
    			int dropChance; 
    			int amount;
    			itemFile = in.readLine(); //File to load item from
    			dropChance = Integer.parseInt(in.readLine());
    			amount = Integer.parseInt(in.readLine());
    			
    			drops.add(new BattleDrop(dropChance, amount, Item.fromFile(itemFile)));
    		}
    		
    		line = in.readLine();
    		numberOfActions = Integer.parseInt(line.substring(line.indexOf(" ")+1));//How many ScriptedActions to load
    		
    		for(int i=0; i<numberOfActions; i++)
    		{
    			actions.add(new ScriptedAction(in.readLine()));//Add actions loaded from the specified filename
    		}
    		in.close();
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found: " + filename);
		}
		catch(Exception e)
		{
			e.printStackTrace();	
		}
		
		image = new CharImage(imageFile);//Create a Image from the file
		image.setTransparent('.');
	}
	public int getBonusDamage() 
	{
		return bonusDamage;
	}
	public void setBonusDamage(int bonusDamage) 
	{
		this.bonusDamage = bonusDamage;
	}
	public int getDefense() 
	{
		return defense;
	}
	public void setDefense(int defense) 
	{
		this.defense = defense;
	}
	public CharImage getImage() 
	{
		return image;
	}
	public void setImage(CharImage image) 
	{
		this.image = image;
	}
	public ArrayList<ScriptedAction> getActions() 
	{
		//System.out.println("Bluh in enemytype.getactions");
		return actions;
		
	}
	public void getDrops(ArrayList<Item> list, int roll) 
	{
		for(BattleDrop d : drops)
		{
			d.getDrops(list, roll);
		}
	}
	public int getHp() 
	{
		return hp;
	}
	public void setHp(int hp) 
	{
		this.hp = hp;
	}
	public int getGoldValue()
	{
		return goldValue;
	}
	public int getXpValue() 
	{
		return xpValue;
	}
	public void setXpValue(int xpValue) 
	{
		this.xpValue = xpValue;
	}
	public String getName() 
	{
		return name;
	}
	public void setName(String name) 
	{
		this.name = name;
	}
}
